﻿using System.IO;
using System.Web;
using System.Xml.Serialization;

namespace ReportsFactory
{
    public class Serializer<T> where T : class, new()
    {
        public bool Serialize(T obj, string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            TextWriter tw = new StreamWriter(HttpContext.Current.Server.MapPath(fileName), false);

            try
            {
                serializer.Serialize(tw, obj);
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                tw.Close();
            }

        }

        public T DeSerialize(string fileName)
        {
            T obj = new T();
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                TextReader tr = new StreamReader(HttpContext.Current.Server.MapPath(fileName));
                obj = (T)serializer.Deserialize(tr);
                tr.Close();
                tr.Dispose();
            }
            catch
            { }

            return obj;
        }
    }
}